package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.Tip;

public class TipDBHelper {
	public static final String TABLE_NAME = "tip_table";
	public static final String TIP_ID = "tip_id";
	public static final String TIP_NAME = "tip_name";
	public static final String TIP_PARAGRAPH = "tip_paragraph";
	public static final String TIP_CONTENT = "tip_content";
	public static final String TIP_DATE = "tip_date";
	public static final String TIP_SAVE = "tip_save";
	public static final String TIP_CATEGORY = "category";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ TIP_ID + " integer primary key ," + TIP_NAME + " text ,"
			+ TIP_PARAGRAPH + " text ," + TIP_CONTENT + " text ," + TIP_DATE
			+ " int ," + TIP_SAVE + " int ," + TIP_CATEGORY + " int " + ");";

	public static void createTip(final Tip tip) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(TIP_ID, tip.getId());
				value.put(TIP_NAME, tip.getName());
				value.put(TIP_PARAGRAPH, tip.getParagraph());
				value.put(TIP_CONTENT, tip.getFulltext());
				value.put(TIP_DATE, tip.getDate());
				value.put(TIP_SAVE, tip.isSave());
				value.put(TIP_CATEGORY, tip.getCategory().toInt());

				resolver.insert(EMarketingContentProvider.TIP_CONTENT_URI,
						value);
			}

		}.start();
	}

	public static void updateTip(Tip tip) {
		ContentValues value = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		value.put(TIP_ID, tip.getId());
		if (tip.getName() != null)
			value.put(TIP_NAME, tip.getName());
		if (tip.getParagraph() != null)
			value.put(TIP_PARAGRAPH, tip.getParagraph());
		if (tip.getFulltext() != null)
			value.put(TIP_CONTENT, tip.getFulltext());
		if (tip.getDate() != 0)
			value.put(TIP_DATE, tip.getDate());

		resolver.update(EMarketingContentProvider.TIP_CONTENT_URI, value,
				TIP_ID + "=?", new String[] { Long.toString(tip.getId()) });

	}

	public static void saveTip(long tipId) {
		ContentValues value = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		value.put(TIP_SAVE, 1);

		resolver.update(EMarketingContentProvider.TIP_CONTENT_URI, value,
				TIP_ID + "=?", new String[] { Long.toString(tipId) });

	}

	public static ArrayList<Tip> getAllTip() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.TIP_CONTENT_URI, new String[] {
						TIP_ID, TIP_PARAGRAPH, TIP_NAME, TIP_DATE }, null,
				null, null);

		if (cursor != null) {
			ArrayList<Tip> tips = new ArrayList<Tip>();
			while (cursor.moveToNext()) {
				Tip tip = new Tip();
				tip.setId(cursor.getLong(cursor.getColumnIndex(TIP_ID)));
				tip.setName(cursor.getString(cursor.getColumnIndex(TIP_NAME)));
				tip.setParagraph(cursor.getString(cursor
						.getColumnIndex(TIP_PARAGRAPH)));
				tip.setDate(cursor.getLong(cursor.getColumnIndex(TIP_DATE)));
				tip.setSave(cursor.getInt(cursor.getColumnIndex(TIP_SAVE)) == 1);
				//tip.setSave(cursor.getString(cursor.getColumnIndex(TIP_SAVE)).toString().equals("True"));
				tip.setCategory(Tip.Category.getCategory(cursor.getInt(cursor
						.getColumnIndex(TIP_CATEGORY))));
				tips.add(tip);
			}
			return tips.size() > 0 ? tips : null;
		}
		return null;
	}

	public static Tip getTipDetail(long id) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();

		Cursor cursor = resolver.query(
				EMarketingContentProvider.TIP_CONTENT_URI, null, TIP_ID + "=?",
				new String[] { Long.toString(id) }, null);

		if (cursor != null && cursor.moveToFirst()) {
			Tip tip = new Tip();
			tip.setId(id);
			tip.setName(cursor.getString(cursor.getColumnIndex(TIP_NAME)));
			tip.setParagraph(cursor.getString(cursor
					.getColumnIndex(TIP_PARAGRAPH)));
			tip.setFulltext(cursor.getString(cursor.getColumnIndex(TIP_CONTENT)));
			tip.setDate(cursor.getLong(cursor.getColumnIndex(TIP_DATE)));
			tip.setSave(cursor.getInt(cursor.getColumnIndex(TIP_SAVE)) == 1);

			cursor.close();
			return tip;
		}
		return null;
	}

}
